package com.skeeey.aim.action.user;

import java.util.List;

import com.skeeey.aim.action.BaseAction;
import com.skeeey.aim.model.Role;
import com.skeeey.aim.model.User;
import com.skeeey.aim.service.UserService;
import com.skeeey.aim.util.AIMUtil;

import org.apache.log4j.Logger;

public class UserAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	
	private Logger logger = Logger.getLogger(UserAction.class);
	
	private User user;
	
	private String userAccount;
	
	private String userPassword;
	
	private String roleName;
	
	private String userListJOSNStr;
	
	private String userId;
	
	private String userName;
	
	private String userTelePhone;
	
	private String userEmail;
	
	private String oldPassword;
	
	private String newPassword;
	
	private String verifiedPassword;
	
	private UserService userService;
	
	public String toLogin() {
		return SUCCESS;
	}
	
	public String login() {
		if(userAccount == null || userAccount.length() == 0) {
			addActionError(getText("error.user.account.empty"));
			return INPUT;
		}
		
		User user = userService.getUserByAccount(userAccount);
		if(user == null) {
			addActionError(getText("error.user.unexitent", new String[] {userAccount}));
			return INPUT;
		}
		
		if(!user.getPassword().equals(userPassword)) {
			addActionError(getText("error.password.wrong"));
			return INPUT;
		}
		
		
		session.setAttribute("user", user);
		Role role = userService.findUserRoleBy(user);
		session.setAttribute("role", role);
		return SUCCESS;
	}
	
	public String logout() {
		session.removeAttribute("user");
		session.removeAttribute("dealerCode");
		session.removeAttribute("timeStamp");
		session.removeAttribute("createTime");
		return SUCCESS;
	}
	
	public String toUpdatePassword() {
		return SUCCESS;
	}
	
	public String updatePassword() {
		User user = (User) session.getAttribute("user");
		if(!user.getPassword().equals(oldPassword)) {
			addActionError(getText("error.password.wrong"));
			return INPUT;
		}
		
		if(verifiedPassword == null || verifiedPassword.length() == 0) {
			addActionError(getText("error.user.password.empty"));
			return INPUT;
		}
		
		if(!verifiedPassword.equals(newPassword)) {
			addActionError(getText("error.user.password.not.equal"));
			return INPUT;
		}
		
		userService.updatePasswordBy(user.getId(), verifiedPassword);
		addActionMessage(getText("success.update.user.password"));
		return SUCCESS;
	}
	
	public String toUserList() {
		List<User> users = userService.getUsers();
		this.userListJOSNStr = AIMUtil.toJSONArrayStr(users);
		return SUCCESS;
	}
	
	public String findUsers() {
		logger.debug(userAccount);
		this.userListJOSNStr = AIMUtil.toJSONArrayStr(userService.findUsersBy(userAccount));
		return SUCCESS;
	}
	
	public String updateUser() {
		userService.updateUserBy(userId, userName, userTelePhone, userEmail, roleName);
		sendResult(0, null);
		return null;
	}
	
	public String deleteUser() {
		userService.deletUserBy(userId);
		sendResult(0, null);
		return null;
	}
	
	public String toAddUser() {
		return SUCCESS;
	}
	
	public String addUser() {
		if(user.getAccount() == null || user.getAccount().length() == 0) {
			addActionError(getText("error.user.account.empty"));
			return INPUT;
		}
		
		if(user.getPassword() == null || user.getAccount().length() == 0) {
			addActionError(getText("error.user.password.empty"));
			return INPUT;
		}
		
		if(userService.getUserByAccount(user.getAccount()) != null) {
			addActionError(getText("error.user.exitent", new String[] {user.getAccount()}));
			return INPUT;
		}
		
		if(!userService.saveUser(user, roleName)) {
			addActionError(getText("error.save.user"));
			return INPUT;
		}
		
		addActionMessage(getText("success.save.user", new String[] {user.getAccount()}));
		return SUCCESS;
	}
	
	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public String getUserPassword() {
		return userPassword;
	}

	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getUserAccount() {
		return userAccount;
	}

	public void setUserAccount(String userAccount) {
		this.userAccount = userAccount;
	}

	public String getUserListJOSNStr() {
		return userListJOSNStr;
	}

	public void setUserListJOSNStr(String userListJOSNStr) {
		this.userListJOSNStr = userListJOSNStr;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserEmail() {
		return userEmail;
	}

	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	}

	public String getUserTelePhone() {
		return userTelePhone;
	}

	public void setUserTelePhone(String userTelePhone) {
		this.userTelePhone = userTelePhone;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getVerifiedPassword() {
		return verifiedPassword;
	}

	public void setVerifiedPassword(String verifiedPassword) {
		this.verifiedPassword = verifiedPassword;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
